package com.ruoyi.web.controller.grade;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.grade.domain.entity.StudentPhysicalGrade;
import com.ruoyi.grade.domain.entity.StudentTotalGrade;
import com.ruoyi.grade.service.IStudentPhysicalGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 学生体测成绩查询控制器
 */
@RestController
@RequestMapping("/system/physical_grade")
@Api(tags = "体测模块")
@PreAuthorize("@ss.hasPermi('student:physicalgrade:controller')")
public class StudentPhysicalGradeController extends BaseController {

    @Autowired
    private IStudentPhysicalGradeService physicalGradeService;

    /**
     * 根据学号查询体测成绩
     */
    @GetMapping("/list")
    public R<List<StudentPhysicalGrade>> getPhysicalGrade(
            @RequestParam String studentId) {
        if(getUserId() != 1202l) studentId = getStudentId();

        List<StudentPhysicalGrade> grades = physicalGradeService.getPhysicalGrade(studentId);
        return grades != null && !grades.isEmpty() ? R.ok(grades) : R.fail("无记录");
    }

    @ApiOperation(value = "导入学生体测成绩")
    @PostMapping("/import")
    @PreAuthorize("@ss.hasPermi('student:physical_grade:import')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "认证码", paramType = "header", required = true),
            @ApiImplicitParam(name = "updateSupport", value = "是否更新支持，如果已存在，则进行更新数据", required = true)
    })
    public AjaxResult importStudentPhysicalGrade(@RequestPart MultipartFile file, boolean updateSupport) throws IOException {
        ExcelUtil<StudentPhysicalGrade> util = new ExcelUtil<StudentPhysicalGrade>(StudentPhysicalGrade.class);
        List<StudentPhysicalGrade> studentPhysicalGradeList = util.importExcel(file.getInputStream());
        String message = physicalGradeService.importStudentPhysicalGrade(studentPhysicalGradeList, updateSupport);
        return success(message);
    }
}